package com.cheyun.netsalev3.view.home

import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.animation.AlphaAnimation
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.adapter.MyPagedListAdapter
import com.cheyun.netsalev3.bean.DaoDianParam
import com.cheyun.netsalev3.bean.YaoyueParam
import com.cheyun.netsalev3.databinding.FragmentItemListBinding
import com.cheyun.netsalev3.utils.BaseFragment
import com.cheyun.netsalev3.utils.api.NetworkState
import com.cheyun.netsalev3.viewmodel.HomeTaskViewModel
import com.jeremyliao.liveeventbus.LiveEventBus


class ItemFragment : BaseFragment<FragmentItemListBinding>() {
    override fun initContentView(): Int {
        return R.layout.fragment_item_list
    }

    override fun initViewModel(): ViewModel? {
        parentFragment?.apply {
            return ViewModelProviders.of(this).get(HomeTaskViewModel::class.java)
        }
        return null
    }


    override fun initView() {
        var recyclerView = binding.root.findViewById<RecyclerView>(R.id.swipe_target)
        recyclerView.layoutManager = LinearLayoutManager(context)
        when (position) {
            //邀约
            1 -> {
                var adapter = MyPagedListAdapter<YaoyueParam>(R.layout.fragment_item, binding.viewModel)
                recyclerView.adapter = adapter
                observeUi1(adapter)
                binding.viewModel?.networkState1?.observe(this, Observer {
                    binding.swipeRefreshLayout.isRefreshing = it == NetworkState.LOADING
                    adapter.setNetworkState(it)
                })

            }
            //到店
            2 -> {
                var adapter = MyPagedListAdapter<DaoDianParam>(R.layout.fragment_item2, binding.viewModel)
                recyclerView.adapter = adapter
                observeUi2(adapter)
                binding.viewModel?.networkState2?.observe(this, Observer {
                    binding.swipeRefreshLayout.isRefreshing = it == NetworkState.LOADING
                    adapter.setNetworkState(it)
                })
            }
        }
        initSwipeToRefresh()
    }

    private fun initSwipeToRefresh() {
        binding.swipeRefreshLayout.setOnRefreshListener {
            binding.viewModel?.refresh(position)
        }
    }

    private fun observeUi1(adapter: MyPagedListAdapter<YaoyueParam>) {
        binding.viewModel?.yaoyuelist?.observe(this, Observer { list ->
            adapter.submitList(list)
        })
        binding.viewModel?.totlelist1?.observe(this, Observer {
            binding.totleText.text = "共"+it.toString() + "条数据"
            showTips()
        })
    }

    private fun observeUi2(adapter: MyPagedListAdapter<DaoDianParam>) {
        binding.viewModel?.daodianlist?.observe(this, Observer { list ->
            adapter.submitList(list)
        })
        binding.viewModel?.totlelist2?.observe(this, Observer {
            binding.totleText.text = "共"+it.toString() + "条数据"
            showTips()
        })
    }

    override fun initData() {
        //ReceptionActivity发送通知 刷新数据
        LiveEventBus.get()
                .with("ReceptionActivity", String::class.java)
                .observe(this, Observer {
                    binding.viewModel?.refresh(position)
                })
        //接受通知消息
        LiveEventBus.get()
                .with("notice", String::class.java)
                .observe(this, Observer {
                    binding?.viewModel?.refresh(position)
                })
    }

    override fun lazyInit() {
        Log.i(TAG,"override lazyinit")
    }

    private var position: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            position = it.getInt("position")
        }
    }

    companion object {
        @JvmStatic
        fun newInstance(position: Int) =
                ItemFragment().apply {
                    arguments = Bundle().apply {
                        putInt("position", position)
                    }
                }
    }

    private fun showTips(){
        binding.totleText.setVisibility(View.VISIBLE)
        binding.totleText.clearAnimation()
        val animation = AlphaAnimation(1f, 0f)
        animation.duration = 3000
        binding.totleText.startAnimation(animation)
        binding.totleText.setVisibility(View.GONE)
    }

}
